package org.example.bootvue.controller;

import jakarta.validation.constraints.Pattern;
import org.example.bootvue.entity.Result;
import org.example.bootvue.entity.User;
import org.example.bootvue.service.IUserService;
import org.example.bootvue.utils.JwtUtil;
import org.example.bootvue.utils.Md5Util;
import org.example.bootvue.utils.ThreadLocalUtil;
import org.hibernate.validator.constraints.URL;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.stereotype.Controller;

import java.time.LocalDateTime;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 用户表 前端控制器
 * </p>
 *
 * @author YourName
 * @since 2025-07-29
 */
@RestController
@RequestMapping("/user")
@CrossOrigin(origins = "http://localhost:5173")
public class UserController {
@Autowired
    IUserService userService;
@PostMapping("/register")
@Validated
    public Result register(@Pattern(regexp = "^\\S{5,16}$") String username,@Pattern(regexp = "^\\S{5,16}$") String password) {
    /*
    if (username!=null && password!=null&&username.length()>=5
            &&password.length()>=5&&username.length()<=16&&password.length()<=16) {
    */    User u=userService.findByUsername(username);
        if (u==null) {
            userService.add(username, password);
            return Result.success();
        }else  return Result.error("用户名已被占用");
    /*}else return Result.error("参数不合法");
繁琐*/
}
@PostMapping("/login")
@Validated
    public Result<String> login(@Pattern(regexp = "^\\S{5,16}$") String username,@Pattern(regexp = "^\\S{5,16}$") String password) {
    //根据用户名查询用户
    User loginuser=userService.findByUsername(username);
    //判断该用户是否存在
    if (loginuser==null) {
        return Result.error("用户名错误");
    }
    //判断密码是否正确 里面password加密的
    if (Md5Util.getMD5String(password).equals(loginuser.getPassword())) {
        Map<String,Object> map=new HashMap<>();
        map.put("id",loginuser.getId());
        map.put("username",loginuser.getUsername());
        String token=JwtUtil.genToken(map);
        return Result.success(token);
    }
    return Result.error("密码错误");
}
@GetMapping("/userInfo")
    public Result<User> userinfo(@RequestHeader(name = "Authorization")String token){
    //根据用户名查询用户
    Map<String, Object> map = ThreadLocalUtil.get();
    String username = (String) map.get("username");
    User byUsername = userService.findByUsername(username);
    return Result.success(byUsername);
}
@PutMapping("/update")
    public Result update(@RequestBody @Validated User user){
    user.setUpdateTime(LocalDateTime.now());
userService.updateById(user);
return Result.success();
}
@PatchMapping("/updateAvatar")
public Result updateAvatar(@RequestParam @URL String avatarurl){
    userService.updateAvatar(avatarurl);
    return Result.success();
}
    @PatchMapping("/updatePwd")
    public Result updatePwd(@RequestBody Map<String,String> map){
    String oldpassword=map.get("old_pwd");
        String newpwd = map.get("new_pwd");
        String rePwd = map.get("re_pwd");
        if(!StringUtils.hasLength(oldpassword)||!StringUtils.hasLength(newpwd)||!StringUtils.hasLength(rePwd)){
            return Result.error("缺少必要的参数");
        }
        Map<String,Object> map1= ThreadLocalUtil.get();
        String username=(String)map1.get("username");
        User loginuser = userService.findByUsername(username);
        if(!loginuser.getPassword().equals(Md5Util.getMD5String(oldpassword))){
            return Result.error("原密码填写不正确");
        }
        if (!rePwd.equals(newpwd))
            return Result.error("两次填写的新密码不一样");
        userService.updatePwd(newpwd);
        return Result.success();
    }
}
